﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmBacf : BaseForm
    {
        private IBLL.ISupply supplyBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();

        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();

        private IBLL.IShop shopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteShop();

        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();

        private IBLL.ISys0 sys0BLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSys0();

        private IBLL.IBacf bacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteBacf();

        public FrmBacf()
        {
            InitializeComponent();
        }


        private void dtpDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtYSUser.Focus();
            }
        }

        private void txtYSUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtSupp.Focus();
            }
        }


        private void txtJHUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtNO_Bei.Focus();
            }
        }

        private void txtNO_Bei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Enabled = false;
                this.txtSupp.Enabled = false;
                this.txtShopCode.Enabled = false;
                this.cBoxDep.Enabled = false;
                this.cBoxSupp.Enabled = false;
                this.dtpDate.Enabled = false;
                this.txtYSUser.Enabled = false;
                this.txtJHUser.Enabled = false;
                this.cBoxShop.Enabled = false;
                this.txtNO_Bei.Enabled = false;

                this.txtID.Enabled = true;
                this.txtSL_P.Enabled = true;
                this.txtUnit_P.Enabled = true;
                this.txtPack.Enabled = true;
                this.txtTax.Enabled = true;
                this.txtSL.Enabled = true;
                this.txtUnit.Enabled = true;
                this.txtPrice.Enabled = true;
                this.txtJe.Enabled = true;
                this.txtCas.Enabled = true;
                this.txtGoodsBei.Enabled = true;
                this.txtNoAmount.Enabled = true;

                this.btnAdd.Enabled = false;
                this.btnAddRecord.Enabled = true;
                this.btnDelRecord.Enabled = true;
                this.btnPrint.Enabled = false;
                this.btnRef.Enabled = true;
                this.btnQuit.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnCan.Enabled = true;
                this.txtID.Focus();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.txtUser.Text = accountUser.UserCode;
            this.txtJHUser.Text = accountUser.UserCode;
            this.txtDate.Text = DateTime.Now.ToString();
            this.txtCode.Clear();
            this.txtPCode.Clear();
            this.txtDepCode.Text = accountUser.DeptCode;
            this.cBoxDep.SelectedValue = accountUser.DeptCode;
            this.dtpDate.Text = DateTime.Now.ToString();
            this.txtYSUser.Text = accountUser.Oper;
            this.cBoxSupp.Text = "";
            this.cBoxShop.Text = "";
            this.txtNO_Bei.Clear();
            this.dataGridView1.Rows.Clear();

            this.txtID.Clear();
            this.txtDM.Clear();
            this.txtGoods.Clear();
            this.txtSL_P.Clear();
            this.txtSL.Clear();
            this.txtPrice.Clear();
            this.txtJe.Clear();
            this.txtNoAmount.Clear();
            this.txtCas.Clear();
            this.txtGoodsBei.Clear();
            this.txtLastPrice.Clear();
            this.txtTotalAmount.Clear();
            this.txtTotalNoAmount.Clear();
            this.txtTotalNum.Clear();
            this.txtTotalQuantity.Clear();
            this.txtUnit_P.Clear();
            this.txtUnit.Clear();
            this.txtPack.Clear();
            this.txtTax.Clear();

            this.txtDepCode.Enabled = true;
            this.txtSupp.Enabled = true;
            this.txtShopCode.Enabled = true;
            this.cBoxDep.Enabled = true;
            this.cBoxSupp.Enabled = true;
            this.dtpDate.Enabled = true;
            this.txtYSUser.Enabled = true;
            this.txtJHUser.Enabled = true;
            this.cBoxShop.Enabled = true;
            this.txtNO_Bei.Enabled = true;
            this.txtDepCode.Focus();

            this.btnAdd.Enabled = false;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;
            this.btnPrint.Enabled = false;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnCan.Enabled = true;

        }

        private void FrmBacf_Load(object sender, EventArgs e)
        {
            //所属部门初始化
            this.cBoxDep.ValueMember = "DEP_ID";
            this.cBoxDep.DisplayMember = "DEPART";
            this.cBoxDep.DataSource = departBLL.GetAll();
            this.cBoxDep.Text = "";

            //仓库初始化
            this.cBoxShop.ValueMember = "SHOP_ID";
            this.cBoxShop.DisplayMember = "SHOP";
            this.cBoxShop.DataSource = shopBLL.GetAll();
            this.cBoxShop.Text = "";

            //供应商初始化
            this.cBoxSupp.ValueMember = "SUPP_ID";
            this.cBoxSupp.DisplayMember = "SUPPLY";
            this.cBoxSupp.DataSource = supplyBLL.GetAll();
            this.cBoxSupp.Text = "";
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtID.Text))
                {
                    MessageBox.Show("请输入合法的商品条码！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (this.txtID.Text.Trim().Length < 4 || this.txtID.Text.Trim().Length > 13)
                {
                    MessageBox.Show(string.Format("当前商品条码只有{0}位，请重新输入4到13位的商品条码！", this.txtID.Text.Trim().Length), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    MisEntity.Goods entity = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (entity == null)
                    {
                        MessageBox.Show("无此商品，请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        this.txtID.Text = entity.ID.Trim();
                        this.txtDM.Text = entity.DM.Trim();
                        this.txtGoods.Text = entity.GOODS.Trim();
                        this.txtUnit_P.Text = entity.UNIT_P.Trim();
                        this.txtPack.Text = entity.PACK.ToString();
                        this.txtTax.Text = entity.Tax.ToString();
                        this.txtUnit.Text = entity.UNIT.Trim();
                        this.txtPrice.Text = String.Format("{0:N4}", entity.Je_last / entity.SL_last);
                        this.txtLastPrice.Text = this.txtPrice.Text;
                        this.txtSL_P.Focus();
                    }

                }
            }
        }

        private void txtSL_P_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtSL_P.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL_P.Text, out n) && decimal.TryParse(this.txtPack.Text, out n))
                    {
                        this.txtSL.Text = String.Format("{0:N2}", decimal.Parse(this.txtPack.Text.Trim()) * decimal.Parse(this.txtSL_P.Text.Trim()));
                        this.txtSL.Focus();
                    }
                }

            }
        }

        private void txtSL_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtPrice.Text) && !string.IsNullOrEmpty(this.txtSL.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPrice.Text, out n))
                    {
                        this.txtJe.Text = String.Format("{0:N2}", decimal.Parse(this.txtPrice.Text.Trim()) * decimal.Parse(this.txtSL.Text.Trim()));
                        
                    }
                }
                this.txtPrice.Focus();
            }
        }

        private void txtJe_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtTax.Text) && !string.IsNullOrEmpty(this.txtJe.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtTax.Text, out n) && decimal.TryParse(this.txtJe.Text, out n))
                    {
                        this.txtNoAmount.Text = String.Format("{0:N2}", decimal.Parse(this.txtJe.Text) / decimal.Parse("1." + this.txtTax.Text.Trim()));
                        this.txtCas.Focus();
                    }
                }
            }
        }

        private void txtCas_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnAddRecord.Focus();
            }
        }

        private void btnAddRecord_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtID.Text))
            {
                MessageBox.Show("请输入合法的商品条码！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtSL.Text))
            {
                MessageBox.Show("请输入数量不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (decimal.Parse(this.txtSL.Text) < 0)
            {
                MessageBox.Show("数量不能为负数！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            String[] DataRow = new String[10];
            DataRow[0] = this.txtID.Text.Trim();
            DataRow[1] = this.txtDM.Text.Trim();
            DataRow[2] = this.txtGoods.Text.Trim();
            DataRow[3] = this.txtUnit.Text.Trim();
            DataRow[4] = this.txtSL.Text.Trim();
            DataRow[5] = this.txtPrice.Text.Trim();
            DataRow[6] = this.txtJe.Text.Trim();
            DataRow[7] = this.txtSL_P.Text.Trim();
            DataRow[8] = this.txtCas.Text.Trim();
            DataRow[9] = this.txtTax.Text.Trim();
            this.dataGridView1.Rows.Add(DataRow);

            //合计计数
            if (string.IsNullOrEmpty(this.txtTotalNum.Text))
            {
                this.txtTotalNum.Text = this.txtSL_P.Text.Trim();
            }
            else
            {
                this.txtTotalNum.Text = (int.Parse(this.txtSL_P.Text.Trim()) + int.Parse(this.txtTotalNum.Text.Trim())).ToString();
            }

            //合计数量
            if (string.IsNullOrEmpty(this.txtTotalQuantity.Text))
            {
                this.txtTotalQuantity.Text = decimal.ToInt32(decimal.Parse(this.txtSL.Text.Trim())).ToString();
            }
            else
            {
                this.txtTotalQuantity.Text = decimal.ToInt32(decimal.Parse(this.txtSL.Text.Trim()) + int.Parse(this.txtTotalQuantity.Text.Trim())).ToString();
            }

            //含税金额
            if (string.IsNullOrEmpty(this.txtTotalAmount.Text))
            {
                this.txtTotalAmount.Text = this.txtJe.Text.Trim();
            }
            else
            {
                this.txtTotalAmount.Text = String.Format("{0:N2}", decimal.Parse(this.txtJe.Text.Trim()) + decimal.Parse(this.txtTotalAmount.Text.Trim()));
            }

            //未税金额
            if (string.IsNullOrEmpty(this.txtTotalNoAmount.Text))
            {
                this.txtTotalNoAmount.Text = this.txtNoAmount.Text.Trim();
            }
            else
            {
                this.txtTotalNoAmount.Text = String.Format("{0:N2}", decimal.Parse(this.txtNoAmount.Text.Trim()) + decimal.Parse(this.txtTotalNoAmount.Text.Trim()));
            }

            this.txtID.Clear();
            this.txtDM.Clear();
            this.txtGoods.Clear();
            this.txtSL_P.Clear();
            this.txtSL.Clear();
            this.txtPrice.Clear();
            this.txtJe.Clear();
            this.txtNoAmount.Clear();
            this.txtCas.Clear();
            this.txtGoodsBei.Clear();
            this.txtID.Focus();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, new SolidBrush(Color.Black), e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误，错误信息：" + ex.Message, "操作失败");
            }
        }

        private void btnDelRecord_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Remove(this.dataGridView1.CurrentRow);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTotalNum.Text))
            {
                MessageBox.Show("合计为零不能保存！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.cBoxDep.Text))
            {
                MessageBox.Show("进货部门不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.cBoxSupp.Text))
            {
                MessageBox.Show("供应商不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtJHUser.Text))
            {
                MessageBox.Show("进货员不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if(this.dataGridView1.Rows.Count >0)
            {
                IList<MisEntity.Bacf> list = new List<MisEntity.Bacf>(this.dataGridView1.Rows.Count);
                MisEntity.Bacf entity = null;
                DataGridViewRow dr = null;

                string pid = sys0BLL.GetNoBac();
                for(int i =0 ;i<this.dataGridView1.Rows.Count;i++){
                    dr = this.dataGridView1.Rows[i];
                    entity = new MisEntity.Bacf();
                    entity.No = pid;
                    entity.ID = dr.Cells["ID"].Value.ToString();
                    entity.DM = dr.Cells["DM"].Value.ToString();
                    entity.Dep_id = this.cBoxDep.SelectedValue.ToString();
                    entity.Supp_id = this.cBoxSupp.SelectedValue.ToString();
                    entity.price = decimal.Parse(dr.Cells["Price"].Value.ToString());
                    entity.TAX = short.Parse(dr.Cells["Tax"].Value.ToString());
                    entity.SL_P = int.Parse(dr.Cells["SL_P"].Value.ToString());
                    entity.UNIT = dr.Cells["UNIT"].Value.ToString();
                    entity.sl = decimal.Parse(dr.Cells["SL"].Value.ToString());
                    entity.je = decimal.Parse(dr.Cells["JE"].Value.ToString());
                    if (!string.IsNullOrEmpty(dr.Cells["Cas"].Value.ToString()))
                    {
                        entity.Cas = decimal.Parse(dr.Cells["Cas"].Value.ToString());
                    }
                    entity.shop_id = this.cBoxShop.SelectedValue.ToString();
                    entity.NO_BEI = this.txtNO_Bei.Text.Trim();
                    list.Add(entity);
                }
                bacfBLL.Insert(list);
                this.txtCode.Text = pid;
                this.txtPCode.Text = pid;
                MessageBox.Show(string.Format("单据已存盘，编号：{0}，总金额：{1}。是否打印[y/n]？", pid, this.txtTotalAmount.Text.Trim()), "提问窗", MessageBoxButtons.OK, MessageBoxIcon.Question);

                this.txtID.Enabled = false;
                this.txtSL_P.Enabled = false;
                this.txtUnit_P.Enabled = false;
                this.txtPack.Enabled = false;
                this.txtTax.Enabled = false;
                this.txtSL.Enabled = false;
                this.txtUnit.Enabled = false;
                this.txtPrice.Enabled = false;
                this.txtJe.Enabled = false;
                this.txtCas.Enabled = false;
                this.txtGoodsBei.Enabled = false;
                this.txtNoAmount.Enabled = false;

                this.btnAdd.Enabled = true;
                this.btnAddRecord.Enabled = false;
                this.btnDelRecord.Enabled = false;
                this.btnPrint.Enabled = true;
                this.btnRef.Enabled = true;
                this.btnQuit.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnCan.Enabled = false;
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            this.txtDepCode.Enabled = false;
            this.txtSupp.Enabled = false;
            this.txtShopCode.Enabled = false;
            this.cBoxDep.Enabled = false;
            this.cBoxSupp.Enabled = false;
            this.dtpDate.Enabled = false;
            this.txtYSUser.Enabled = false;
            this.txtJHUser.Enabled = false;
            this.cBoxShop.Enabled = false;
            this.txtNO_Bei.Enabled = false;

            this.txtID.Enabled = false;
            this.txtSL_P.Enabled = false;
            this.txtUnit_P.Enabled = false;
            this.txtPack.Enabled = false;
            this.txtTax.Enabled = false;
            this.txtSL.Enabled = false;
            this.txtUnit.Enabled = false;
            this.txtPrice.Enabled = false;
            this.txtJe.Enabled = false;
            this.txtCas.Enabled = false;
            this.txtGoodsBei.Enabled = false;
            this.txtNoAmount.Enabled = false;

            this.btnAdd.Enabled = true;
            this.btnAddRecord.Enabled = false;
            this.btnDelRecord.Enabled = false;
            this.btnPrint.Enabled = true;
            this.btnRef.Enabled = true;
            this.btnQuit.Enabled = false;
            this.btnSave.Enabled = false;
            this.btnCan.Enabled = false;
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    this.cBoxDep.SelectedValue = this.txtDepCode.Text.Trim();
                    if (this.cBoxDep.SelectedValue == null || this.cBoxDep.SelectedValue.ToString().Trim().Length < 1)
                    {
                        MessageBox.Show("部门代码应为1位以上,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtDepCode.Focus();
                    }
                    else
                    {
                        this.dtpDate.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                }

            }
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
                {
                    this.cBoxShop.SelectedValue = this.txtShopCode.Text.Trim().ToUpper();
                    if (this.cBoxShop.SelectedValue == null)
                    {
                        MessageBox.Show("进货仓库代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtShopCode.Focus();
                    }
                    else
                        this.txtJHUser.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtShopCode.Focus();
                }

            }
        }

        private void txtSupp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtSupp.Text.Trim()))
                {
                    this.cBoxSupp.SelectedValue = this.txtSupp.Text.Trim().ToUpper();
                    if (this.cBoxSupp.SelectedValue == null)
                    {
                        MessageBox.Show("供应商代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtSupp.Focus();
                    }
                    else
                        this.txtShopCode.Focus();
                }
                else
                {
                    MessageBox.Show("不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtSupp.Focus();
                }

            }
        }

        private void cBoxDep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxDep.SelectedValue != null)
                this.txtDepCode.Text = this.cBoxDep.SelectedValue.ToString().Trim();
        }

        private void cBoxShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxShop.SelectedValue != null)
                this.txtShopCode.Text = this.cBoxShop.SelectedValue.ToString().Trim();
        }

        private void cBoxSupp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cBoxSupp.SelectedValue != null)
                this.txtSupp.Text = this.cBoxSupp.SelectedValue.ToString().Trim();
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtPrice.Text) && !string.IsNullOrEmpty(this.txtSL.Text))
                {
                    decimal n;
                    if (decimal.TryParse(this.txtSL.Text, out n) && decimal.TryParse(this.txtPrice.Text, out n))
                    {
                        this.txtJe.Text = String.Format("{0:N2}", decimal.Parse(this.txtPrice.Text.Trim()) * decimal.Parse(this.txtSL.Text.Trim()));
                    }
                }
                this.txtJe.Focus();
            }
        }

        private void cBoxSupp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtShopCode.Focus();
            }
        }

    }
}
